package module.scene.view.iso
{
	import com.darwin.darwinBitmapEngine.core.Node;
	import com.darwin.darwinDisplay.DarBitmapMovieNode;
	import com.darwin.darwinDisplay.DarBitmapMovieNodeEvent;
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinDll.core.DllItem;
	import com.darwin.darwinIsometric.core.Point3D;
	import com.darwin.darwinIsometric.utils.IsoUtils;
	import com.darwin.darwinSimpleShape.Cube;
	import com.darwin.darwinUtils.BitmapDataTool;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import module.scene.consts.SceneConst;
	import module.scene.model.dataObj.IsoDataObj;
	import module.scene.view.iso.base.JusticeIsoObject;
	
	/**
	 *	领地 
	 * @author Roland
	 * 
	 */
	public class TerritoryIsoObject extends JusticeIsoObject
	{
		private var _range:int = 40;//领地所占范围
		public function TerritoryIsoObject(isoDataObj:IsoDataObj, dataObj:Object=null)
		{
			super(isoDataObj, dataObj);
			clickEnabled = true;
			mouseHoverEnabled = true;
			hideWithFog = false;
			init();
		}
		
		private var _node:DarBitmapMovieNode;
		private var _rangeNode:Node;
		private var _gridNode:Node;
		private function init():void{
			Dll.getInstance().getMatRes(_serverId,onMatComplete,"MATERIAL_" + _serverId,0,DllItem.TYPE_SWF);
		}
		
		private function onMatComplete(data:Class):void
		{
			if(disposed==true)return;//物件已被销毁,不做后续处理
			_node = new DarBitmapMovieNode(data);
			_node.addEventListener(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE,onDrawComplete);
			addNode(_node); 
			
			var $rangeCube:Shape = new Shape();
			drawRange($rangeCube.graphics,SceneConst.GRID_SIZE*_range,SceneConst.GRID_SIZE*_range,0xff6600,0.8);
			_rangeNode = new Node(BitmapDataTool.getBitmapData($rangeCube));
			_rangeNode.x = -SceneConst.GRID_SIZE*_range ;	
			_rangeNode.y = 0;
			addNodeAt(_rangeNode,0);

			var $gridCube:Shape = new Shape();
			drawGrid($gridCube.graphics,SceneConst.GRID_SIZE*_range,SceneConst.GRID_SIZE*_range,0xffffff,0.2);
			_gridNode = new Node(BitmapDataTool.getBitmapData($gridCube));
			_gridNode.x = -SceneConst.GRID_SIZE*_range ;	
			_gridNode.y = 0;
			_gridNode.visible = _gridVisible;
			addNodeAt(_gridNode,0);
		}
		
		protected function onDrawComplete(event:DarBitmapMovieNodeEvent):void
		{
			_node.removeEventListener(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE,onDrawComplete);
			_node.x = _node.offsetPoint.x;
			_node.y = _node.offsetPoint.y;
		}
		
		private function drawRange(graphics:Graphics,rangeWidth:Number,rangeHeight:Number,color:uint = 0xff0000,alpha:Number = 1):void
		{
			var pt:Point;
			graphics.clear();
			graphics.lineStyle(4,color,alpha);
			pt = IsoUtils.isoToScreen(new Point3D(0,0,0));
			graphics.moveTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(rangeWidth,0,0));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(rangeWidth,0,rangeWidth));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(0,0,rangeWidth));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(0,0,0));
			graphics.lineTo(pt.x,pt.y);
		}
		
		private function drawGrid(graphics:Graphics,rangeWidth:Number,rangeHeight:Number,color:uint = 0xff0000,alpha:Number = 1):void
		{
			var pt:Point;
			graphics.clear();
			graphics.lineStyle(4,color,alpha);
//			graphics.beginFill(color,alpha/4);
			/*pt = IsoUtils.isoToScreen(new Point3D(0,0,0));
			graphics.moveTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(rangeWidth,0,0));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(rangeWidth,0,rangeWidth));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(0,0,rangeWidth));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(0,0,0));
			graphics.lineTo(pt.x,pt.y);*/
			for (var x:int = 0; x <= rangeWidth; x+=SceneConst.GRID_SIZE) 
			{
				pt = IsoUtils.isoToScreen(new Point3D(x,0,0));
				graphics.moveTo(pt.x,pt.y);
				pt = IsoUtils.isoToScreen(new Point3D(x,0,rangeWidth));
				graphics.lineTo(pt.x,pt.y);
			}
			for (var z:int = 0; z <= rangeWidth; z+=SceneConst.GRID_SIZE) 
			{
				pt = IsoUtils.isoToScreen(new Point3D(0,0,z));
				graphics.moveTo(pt.x,pt.y);
				pt = IsoUtils.isoToScreen(new Point3D(rangeWidth,0,z));
				graphics.lineTo(pt.x,pt.y);
			}
		}
		
		private var _gridVisible:Boolean = false;
		
		/**
		 *	显示领地范围
		 */
		public function showRangeGrid():void
		{
			_gridVisible = true;
			if(_gridNode)
			{
				_gridNode.visible = _gridVisible;
			}
		}
		
		/**
		 *隐藏领地范围 
		 * 
		 */
		public function hideRangeGrid():void
		{
			_gridVisible = false;
			if(_gridNode)
			{
				_gridNode.visible = _gridVisible;
			}
		}
		
		override public function updateFog():void
		{
			return;//领地本身不驱散迷雾
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(_node)
			{
				_node.destroy();
				_node = null;
			}
		}
	}
}